/*
 * SystemTimer.cpp
 *
 *  Created on: 2009/01/12
 *      Author: elecgon
 */

#include "SystemTimer.h"

template<> SystemTimer* ISingleton<SystemTimer>::m_pInstance = 0;

#ifdef _WIN32

#	include <mmsystem.h>
#	pragma comment(lib, "winmm.lib")

#else
	#include <sys/time.h>

	//typedef int DWORD;
	int timeGetTime()
	{
		static int start=0;
		static struct timeval t;
		gettimeofday(&t, NULL);
		if (start==0){
			start = t.tv_sec;
		}

		return (int)((t.tv_sec-start)*1000 + t.tv_usec/1000);
	}

#endif

SystemTimer::SystemTimer()
{
	// TODO Auto-generated constructor stub
	mLastTime = timeGetTime();

}

SystemTimer::~SystemTimer()
{
	// TODO Auto-generated destructor stub
}

uint32 SystemTimer::getTimeElapsed(uint32 now, uint32 prev)
{
	if(prev > now)
		return ((uint32)~0) - prev + now;

	return now - prev;
}

uint32 SystemTimer::getElapsed()
{
	uint32 theTime = timeGetTime();
	return getTimeElapsed(theTime, mLastTime);
}
